import { defineStore } from 'pinia';
import { reactive } from 'vue';
import type { ProductForm } from '@/types/product';

export const useProductInfo = defineStore(
  'productInfo',
  () => {
    const state = reactive<{ productForm: ProductForm }>({
      productForm: {
        pid: '',
        title: '', // 产品名称
        images: '', // 轮播图
        memo: '', // 备注
        category: '', // 分类
        paypassword: '', // 支付密码
      },
    });
    const copyProductForm = JSON.stringify(state.productForm);

    function setProductForm(value: Partial<ProductForm>) {
      Object.assign(state.productForm, value);
    }

    function clearProductForm() {
      state.productForm = JSON.parse(copyProductForm);
    }

    return { state, setProductForm, clearProductForm };
  },
  {
    persist: true,
  }
);
